define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    import_url: 'coupon/import',
                    dragsort_url:'',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"discount":__('折扣'),"reward":__('满减'),"goods":__('赠品')}, formatter: Table.api.formatter.status},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'LIKE'},
                        {field: 'money', title: __('优惠金额/折扣/商品'),formatter:function (val,data) {
                                if (data.type == 'reward'){
                                    return data.money;
                                } else if(data.type == 'discount'){
                                    return data.discount+'折';
                                } else {
                                    return data.goods.title;
                                }
                        }},
                        {field: 'count', title: __('Count')},
                        {field: 'lead_count', title: __('剩余数量'),formatter:function (val,data) {
                                return data.count-data.lead_count;
                        }},

                        {field: 'max_fetch', title: __('Max_fetch')},
                        {field: 'end_time', title: __('有效期限'),formatter:function (val,data) {
                            if (data.validity_type == 0){
                                //const date = new Date(data.end_time);
                                //return date.toLocaleString();
                                return '失效期'+data.end_time_text;
                            } else {
                                return '领取后'+data.fixed_term+'天有效';
                            }
                        }},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        //{field: 'status', title: __('Status'), searchList: {"1":__('进行中'),"2":__('已结束'),"-1":__('已关闭')}, formatter: Table.api.formatter.status},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'coupon/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'coupon/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'coupon/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                //优惠券类型切换
                var type = $('input[name="row[type]"]:checked').val();

                if (type=='reward'){
                    $(".reward").show();
                    $(".discount").hide();
                    $(".goods").hide();
                } else if(type=='discount'){
                    $(".discount").show();
                    $(".reward").hide();
                    $(".goods").hide();
                } else {
                    $(".goods").show();
                    $(".reward").hide();
                    $(".discount").hide();
                }

                $(document).on("click", "input[type=radio][name='row[type]']",
                    function(){

                        if($(this).val()=='reward'){
                            $(".reward").show();
                            $(".discount").hide();
                            $(".goods").hide();
                            $("#c-money").attr('data-rule','required');
                            $("#c-discount").removeAttr('required');
                            $("#c-discount_limit").removeAttr('required');
                            //$("#c-goods_id").removeAttr('required');
                            //$("#c-goods_id").removeAttr('aria-invalid');
                        }else if($(this).val()=='discount'){
                            $(".discount").show();
                            $(".reward").hide();
                            $(".goods").hide();
                            $("#c-discount").attr('data-rule','required');
                            $("#c-discount_limit").attr('data-rule','required');
                            $("#c-money").removeAttr('required');
                            //$("#c-goods_id").removeAttr('required');
                            //$("#c-goods_id").removeAttr('aria-invalid');
                            //$("#c-goods_id").attr('data-rule','');
                        } else  if($(this).val()=='goods'){
                            $(".goods").show();
                            $(".reward").hide();
                            $(".discount").hide();
                            //$("#c-goods_id").attr('data-rule','required');
                            $("#c-discount").removeAttr('required');
                            $("#c-discount_limit").removeAttr('required');
                            $("#c-money").removeAttr('required');
                        }
                    });
                //时间切换
                var val = $('input[name="row[validity_type]"]:checked').val();

                if (val==0){
                    $("#end_time").show();
                    $("#fixed_term").hide();
                } else {
                    $("#fixed_term").show();
                    $("#end_time").hide();
                }

                $(document).on("click", "input[type=radio][name='row[validity_type]']",
                    function(){
                        if($(this).val()==1){
                            $("#fixed_term").show();
                            $("#end_time").hide();
                            //$("#c-end_time").removeAttr('data-rule');
                            $("#c-end_time").removeAttr('required');
                            $("#c-fixed_term").attr('data-rule','required');
                        }else{
                            $("#end_time").show();
                            $("#fixed_term").hide();
                            //$("#c-fixed_term").removeAttr('data-rule');
                            $("#c-fixed_term").removeAttr('required');
                            $("#c-end_time").attr('data-rule','required');
                        }
                    });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
